<?php
namespace app\qianyu\controller;
use think\Controller;
use think\Validate;
use think\Db;
use org\BaiduMap;
use think\Model;
use think\Loader;
use org\HMongodb;
class Auto extends Controller
{
        //帮我买   帮我送   帮我办  3小时未接单     自动取消
    public function QueryRrbData()
    {
        $field = [
            'a.id','a.begin', 'a.end','a.cid', 'uid','base_price','did','a.time', 'order_id','avatar','b.evaluationScore','a.type','a.number','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin',  'a.service_type','is_new_order','a.status','total_price','couponid'
        ];
        $result = Db::table('slzd_order_rrb a')->join('slzd_user b','a.uid = b.id')
            ->where('(a.status = 1 ) or (a.status = 3 and is_new_order = 1 )')
            ->where(['orders_type' => 0])
            ->field($field)
            ->select();
        if (!$result){
            echo ' 没有数据要执行';
        } else {
            foreach ($result as $key => $value) {
                if($value['service_type'] == 1){
                    if (($value['time'] + 3 * 60 * 60) < time()) {
                        $this->ToCancelOrder($value);
                    }
                }else{
                    if($value['time'] + 60 * 60 < time()){
                        $this->ToCancelOrder($value);
                    }
                }
            }
        }
    }
    
    // //帮我买   帮我送   帮我办  3小时未接单     自动取消
    public function ToCancelOrder($order_data){
            if($order_data['status'] == 3 && $order_data['is_new_order'] == 1){
                Db::startTrans();
                try{
                    if($order_data['couponid'] > 0){
                            $expiration = db::name('coupon')->where('id',$order_data['couponid'])->value('expiration');
                            if(time() > ($expiration - 60*60*24)){
                                $call_coupon = db::name('coupon')->where('id',$order_data['couponid'])->update(['status'=>2,'expiration'=>$expiration + 60*60*24]);
                            }else{
                                $call_coupon = db::name('coupon')->where('id',$order_data['couponid'])->update(['status'=>2]);
                            }
                        }else{
                            $call_coupon = true;
                        }
                $res = Db::table('slzd_order_rrb')
                    ->where(['id' => $order_data['id']])
                    ->update(['status' => 0, 'time0' => time(), 'cancel_content' => '订单已经过期','cancel_type'=>0,'updated_at' => time()]);
                $data['balance'] = Db::table('slzd_user')->where('id', $order_data['uid'])->value('balance');
                $user_res = Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('balance',$order_data['total_price']);
                $data['money'] = $order_data['total_price'];
                $data['uid'] = $order_data['uid'];
                $data['type'] = 4;
                $data['status'] = 1;
                $data['order'] = $this->generate();
                $data['orderid'] = $order_data['id'];
                $data['ordertype'] = 'rro';
                $data['created_at'] = time();
                $data['updated_at'] = time();
                $data['balance'] = $data['balance']+$data['money'];
                $user_money = Db::table('slzd_user_money_log')->insert($data);
                if($user_res && $user_money && $res && $call_coupon){
                            db::commit();
                        }else{
                            throw new Exception("Error");
                        }
            }catch(\Exception $e){
                db::rollback();
            }
        }else{
           $res = Db::table('slzd_order_rrb')
            ->where(['id' => $order_data['id']])
            ->update(['status' => 0, 'time0' => time(), 'cancel_content' => '订单已经过期','cancel_type'=>0,'updated_at' => time()]); 
        }
        $alias['u'] = Db::table('slzd_user')->where(['id'=>$order_data['uid']])->value('alias');
        if($alias){
            sendPush([
                'type' => 'u',
                'Content' => '您的订单已过期',
                'Title' => '您的订单已过期',
                'alias' => $alias['u'],
                'data' => [
                    'code' => '66817',// 人人帮下单
                    //'data' => $order_data,
                    'data' => '',
                    'sound' => '您的订单已过期'
                ],
                'extras' => []
            ]);
        }
    }
    // 帮我买   帮我送   帮我办  3小时未接单     自动取消
    // public function QueryRrbData()
    // {
    //     $field = [
    //         'a.id','a.begin', 'a.end','a.cid', 'uid','base_price','did','a.time', 'order_id','avatar','b.evaluationScore','a.type','a.number','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin',  'a.service_type'
    //     ];
    //     $result = Db::table('slzd_order_rrb a')->join('slzd_user b','a.uid = b.id')
    //         ->where(['a.status' => 1])
    //         ->where(['orders_type' => 0])
    //         ->field($field)
    //         ->select();
    //     if (!$result){
    //         echo ' 没有数据要执行';
    //     } else {
    //         foreach ($result as $key => $value) {
    //             if($value['service_type'] == 1){
    //             if (($value['time'] + 3 * 60 * 60) < time()) {
    //                 //$userid = $value['uid'];
    //                 //$id = $value['id'];
    //                 $this->ToCancelOrder($value);
    //             }
    //         }else{
    //                 if($value['time'] + 60 * 60 < time()){
    //                     $this->ToCancelOrder($value);
    //                 }
    //             }
    //         }

    //     }
    // }
    // public function QueryRrbData()
    // {
    //     $field = [
    //         'a.id','a.begin', 'a.end','a.cid', 'uid','base_price','did','a.time', 'order_id','avatar','b.evaluationScore','a.type','a.number','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin',  'a.service_type','is_new_order','a.status','total_price'
    //     ];
    //     $result = Db::table('slzd_order_rrb a')->join('slzd_user b','a.uid = b.id')
    //         ->where('(a.status = 1 ) or (a.status = 3 and is_new_order = 1 )')
    //         ->where(['orders_type' => 0])
    //         ->field($field)
    //         ->select();
    //     if (!$result){
    //         echo ' 没有数据要执行';
    //     } else {
    //         foreach ($result as $key => $value) {
    //             if($value['service_type'] == 1){
    //                 if (($value['time'] + 3 * 60 * 60) < time()) {
    //                     //$userid = $value['uid'];
    //                     //$id = $value['id'];
    //                     $this->ToCancelOrder($value);
    //                 }
    //             }else{
    //                 if($value['time'] + 60 * 60 < time()){
    //                     $this->ToCancelOrder($value);
    //                 }
    //             }
    //         }
    //     }
    // }


    // 帮我买   帮我送   帮我办  3小时未接单     自动取消
    // public function ToCancelOrder($order_data){
    //     $res = Db::table('slzd_order_rrb')
    //         ->where(['id' => $order_data['id']])
    //         ->update(['status' => 0, 'time0' => time(), 'cancel_content' => '订单已经过期','cancel_type'=>0,'updated_at' => time()]);
    //     $alias['u'] = Db::table('slzd_user')->where(['id'=>$order_data['uid']])->value('alias');
    //     if($alias){
    //         sendPush([
    //             'type' => 'u',
    //             'Content' => '您的订单已过期',
    //             'Title' => '您的订单已过期',
    //             'alias' => $alias['u'],
    //             'data' => [
    //                 'code' => '66817',// 人人帮下单
    //                 //'data' => $order_data,
    //                 'data' => '',
    //                 'sound' => '您的订单已过期'
    //             ],
    //             'extras' => []
    //         ]);
    //     }
    // }

    // public function ToCancelOrder($order_data){
    //         if($order_data['status'] == 3 && $order_data['is_new_order'] == 1){
    //             Db::startTrans();
    //             try{
    //             $res = Db::table('slzd_order_rrb')
    //                 ->where(['id' => $order_data['id']])
    //                 ->update(['status' => 0, 'time0' => time(), 'cancel_content' => '订单已经过期','cancel_type'=>0,'updated_at' => time()]);
    //             $data['balance'] = Db::table('slzd_user')->where('id', $order_data['uid'])->value('balance');
    //             $user_res = Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('balance',$order_data['total_price']);
    //             $data['money'] = $order_data['total_price'];
    //             $data['uid'] = $order_data['uid'];
    //             $data['type'] = 4;
    //             $data['status'] = 1;
    //             $data['order'] = $this->generate();
    //             $data['orderid'] = $order_data['id'];
    //             $data['ordertype'] = 'rrb';
    //             $data['created_at'] = time();
    //             $data['updated_at'] = time();
    //             $data['balance'] = $data['balance']+$data['money'];
    //             $user_money = Db::table('slzd_user_money_log')->insert($data);
    //             if($user_res && $user_money && $res){
    //                         db::commit();
    //                     }else{
    //                         throw new Exception("Error");
    //                     }
    //         }catch(\Exception $e){
    //             db::rollback();
    //         }
    //     }else{
    //        $res = Db::table('slzd_order_rrb')
    //         ->where(['id' => $order_data['id']])
    //         ->update(['status' => 0, 'time0' => time(), 'cancel_content' => '订单已经过期','cancel_type'=>0,'updated_at' => time()]); 
    //     }
    //     $alias['u'] = Db::table('slzd_user')->where(['id'=>$order_data['uid']])->value('alias');
    //     if($alias){
    //         sendPush([
    //             'type' => 'u',
    //             'Content' => '您的订单已过期',
    //             'Title' => '您的订单已过期',
    //             'alias' => $alias['u'],
    //             'data' => [
    //                 'code' => '66817',// 人人帮下单
    //                 //'data' => $order_data,
    //                 'data' => '',
    //                 'sound' => '您的订单已过期'
    //             ],
    //             'extras' => []
    //         ]);
    //     }
    // }

    //帮我买   帮我送    帮我办   30分钟未支付     自动取消
    public function QueryPayData()
    {
        $field = [
            'a.id','a.begin', 'start_province','start_city','time0','start_detail','start_dist','a.end','a.cid','buy_type', 'tip','content','time2','orders_type','uid','base_price','did','a.time', 'order_id','avatar','b.evaluationScore','a.type','a.number','a.category_name', 'b.nickname', 'time0','b.gender', 'b.mobile', 'a.begin',  'a.service_type'
        ];
        $result = Db::table('slzd_order_rrb a')
            ->join('slzd_user b','a.uid = b.id')
            ->where(['a.status' => 2])
            ->field($field)
            ->select();
        if (!$result){
            echo ' 没有数据要执行';
        } else {
            foreach ($result as $key => &$value) {
                $value['showtime'] = $value['service_type'] == 1 ? '立即送达' : date('Y-m-d H:i:s', $value['time']);
                $value['showtip'] = $value['tip'] / 100 == 0 ? 0 : ($value['tip'] / 100);
                $value['showprice'] = ($value['base_price'] / 100);
                $value['shownumber'] = $value['number'] == '' ? '' : $value['number'] . '公斤';
                $value['content'] = $value['content'] . $value['shownumber'];
                $value['showbuy_type'] = $value['buy_type'] == 1 ? '自行选择购买地' : ($value['buy_type'] == 2 ? '指定地址购买' : '');
                $value['start_address'] = $value['start_province'] . $value['start_city'] . $value['start_dist'] . $value['start_detail'];
                if (($value['time2'] + 30*60) < time()) {
                    $this->ToCancelPayOrder($value);
                }
            }
        }
    }
    //帮我买   帮我送   帮我办  30分钟未支付     自动取消
    public function ToCancelPayOrder($order_data){
        $res = Db::table('slzd_order_rrb')
            ->where(['id' => $order_data['id']])
            ->update(['status' => 0, 'time0' => time(),'updated_at'=>time() ,'cancel_content' => '订单已经过期','cancel_type'=>0,]);
        if($order_data['orders_type'] ==1){
            $alias['d'] = Db::table('slzd_driver')->where(['driverId'=>$order_data['did']])->value('alias');
            if($alias['d']) {
                aliPush([
                    'alias' => $alias['d'],
                    'title' => '用户未支付，订单已过期',
                    'body' => '用户未支付，订单已过期',
                    'code' => '66817',// 人人帮下单
                    'data' => [
                        'code' => '66817',// 人人帮下单
                        'data' => $order_data,
                        'sound' => '用户未支付，订单已过期'
                    ],
                ]);// 阿里云推送给d端用户
            }
            $alias['u'] = Db::table('slzd_user')->where(['id'=>$order_data['uid']])->value('alias');
            if($alias['u']){
                sendPush([
                    'type' => 'u',
                    'Content' => '您的订单已过期',
                    'Title' => '您的订单已过期',
                    'alias' => $alias['u'],
                    'data' => [
                        'code' => '66817',// 人人帮下单
                        'data' => $order_data,
                        'sound' => '您的订单已过期'
                    ],
                    'extras' => []
                ]);
            }
        }else{
            $alias['d'] = Db::table('slzd_user')->where(['id'=>$order_data['did']])->value('alias');
            $alias['u'] = Db::table('slzd_user')->where(['id'=>$order_data['uid']])->value('alias');
            if($alias['d']){
                sendPush([
                    'type' => 'u',
                    'Content' => '用户未支付，订单已过期',
                    'Title' => '用户未支付，订单已过期',
                    'alias' => $alias['d'],
                    'data' => [
                        'code' => '66817',// 人人帮下单
                        'data' => '',
                        'sound' => '用户未支付，订单已过期'
                    ],
                    'extras' => []
                ]);
            }
            if($alias['u']){
                sendPush([
                    'type' => 'u',
                    'Content' => '您的订单已过期',
                    'Title' => '您的订单已过期',
                    'alias' => $alias['u'],
                    'data' => [
                        'code' => '66817',// 人人帮下单
                        'data' => '',
                        'sound' => '您的订单已过期'
                    ],
                    'extras' => []
                ]);
            }
        }
    }
    //帮我赚    30分钟未支付     自动取消
    public function QueryBwzPayData()
    {
        $field = [
            'a.id','a.begin', '"" start_province','"" start_city','"" start_detail','""  start_dist','a.end','a.cid', 'tip','content','time2','uid','base_price','did','a.time', 'order_id','avatar','b.evaluationScore','a.type','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin',  'a.service_type'
        ];
        $result = Db::table('slzd_order_bwz a')
            ->join('slzd_user b','a.uid = b.id')
            ->where(['a.status' => 2])
            ->field($field)
            ->select();
        if (!$result){
            echo ' 没有数据要执行';
        } else {
            foreach ($result as $key => &$value) {
                $value['showtime'] = $value['service_type'] == 1 ? '立即送达' : date('Y-m-d H:i:s', $value['time']);
                $value['showtip'] = $value['tip'] / 100 == 0 ? 0 : ($value['tip'] / 100);
                $value['showprice'] = ($value['base_price'] / 100);
                $value['start_address'] = $value['start_province'] . $value['start_city'] . $value['start_dist'] . $value['start_detail'];

                if (($value['time2'] + 30*60) < time()) {
                    $this->ToCancelPayBwzOrder($value);
                }
            }
        }
    }
     public function ToCancelPayBwzOrder($order_data){
         $res = Db::table('slzd_order_bwz')
             ->where(['id' => $order_data['id']])
             ->update(['status' => 0, 'time0' => time(),'updated_at'=>time()]);

         $alias['d'] = Db::table('slzd_user')->where(['id'=>$order_data['did']])->value('alias');
         $alias['u'] = Db::table('slzd_user')->where(['id'=>$order_data['uid']])->value('alias');
         if($alias['d']){
             sendPush([
                 'type' => 'u',
                 'Content' => '用户未支付，订单已过期',
                 'Title' => '用户未支付，订单已过期',
                 'alias' => $alias['d'],
                 'data' => [
                     'code' => '66817',// 人人帮下单
                     'data' => '',
                     'sound' => '用户未支付，订单已过期'
                 ],
                 'extras' => []
             ]);
         }
         if($alias['u']){
             sendPush([
                 'type' => 'u',
                 'Content' => '您的订单已过期',
                 'Title' => '您的订单已过期',
                 'alias' => $alias['u'],
                 'data' => [
                     'code' => '66817',// 人人帮下单
                     'data' => '',
                     'sound' => '您的订单已过期'
                 ],
                 'extras' => []
             ]);
         }
     }

        /*
    *帮我买    帮我办    帮我送   未完成的订单
    * */
    public function QueryRrbIncomplete()
    {
        $field = [
            'a.id','a.begin','time4', 'a.end','a.cid', 'time2','orders_type','uid','tip','base_price','did','a.time', 'order_id','avatar','b.evaluationScore','a.type','a.number','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin',  'a.service_type','a.cid','time4','buy_type'
        ];
        $result = Db::table('slzd_order_rrb a')
            ->join('slzd_user b','a.uid = b.id')
            ->where(['a.status' => 4])
            ->field($field)
            ->select();
        if (!$result) {
            echo ' 没有数据要执行';
        } else {
            foreach ($result as $key => $value) {
                if (($value['time4'] + 30*60) < time()) {
                    //未确认的订单
                    $this->ToCompleteOrder($value);
                }
            }
        }
    }
    /*
   *帮我买    帮我办    帮我送   未完成的订单
   * */
    public function ToCompleteOrder($order_res){
        if($order_res['orders_type'] == 1){
            if($order_res['cid'] == 33){
                            Db::startTrans();
            try{
                $field = [
                'a.id,a.begin', 'tip','a.end','a.cid', 'uid','base_price','did','a.time', 'a.order_id','avatar','b.evaluationScore','a.type','a.number','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin', 'b.avatar', 'a.service_type','buy_type','time4','oid'
            ];

            $order_data = Db::name('order_rrb a')
                ->join('slzd_user b','a.uid = b.id')
                ->field($field)
                ->where(['a.id' => $order_res['id']])->order('a.id desc')->find();
                $order_data['base_price'] = $order_data['base_price'] - $order_data['tip'];
            $order_data['time'] = $order_data['service_type'] == 1 ? '立即送达' : date('Y-m-d H:i:s',$order_data['time']);
            $order_data['avatar'] = config('path.userimg').$order_data['avatar'];
            $order_data['title'] = '用户已经确认完成';
            $alias = Db::table('slzd_driver')->where(['driverId'=>$order_data['did']])->value('alias');
            //给司机加钱
            $data['balance'] = Db::table('slzd_driver')->where('driverId', $order_data['did'])->value('balance');
            Db::table('slzd_driver')->where('driverId', $order_data['did'])->setInc('carpoolTimes', 1);
            Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('orderNum', 1);
            Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('point',floor($order_data['base_price']/100));
                if($order_data['type'] == 2){
                    $data['money'] = ($order_data['base_price'])/100*100 + $order_data['tip'];
                    }else{
                        if($order_data['type'] == 1 && $order_data['buy_type'] == 3){
                            $data['money'] = ($order_data['base_price'])/100*100;
                        }else{
                            $data['money'] = ($order_data['base_price'])/100*100 + $order_data['tip'];
                        }
                    }
            $data['driverId'] = $order_data['did'];
            $data['type'] = 1;
            $data['correlationName'] = '订单收入';
            $data['correlationCode'] = $order_data['order_id'];
            $data['created_at'] = $order_data['time4'] + 30*60;
            $data['updated_at'] = $order_data['time4'] + 30*60;
            $data['balance'] = $data['balance'] + $data['money'];
            Db::table('slzd_driver_money_log')->insert($data);
            Db::table('slzd_driver')->where('driverId', $order_data['did'])->setInc('balance', $data['money']);
            if($order_data['cid'] == ''){
                $order_data['cid'] == 1;
            }
            $balance = Db::table('slzd_company')->where('id',$order_data['cid'])->value('balance');
            $c_data['money'] = ($order_data['base_price'])*0/100;
            $c_data['companyId'] = $order_data['cid'];
            $c_data['type'] = 1;
            $c_data['correlationName'] = '订单收入';
            $c_data['correlationCode'] = $order_data['order_id'];
            $c_data['created_at'] = $order_data['time4'] + 30*60;
            $c_data['updated_at'] = $order_data['time4'] + 30*60;
            $c_data['balance'] = $balance + $c_data['money'];
            Db::table('slzd_company_money_log')->insert($c_data);
           //给代理加钱
            Db::table('slzd_company')->where('id', $order_data['cid'])->setInc('balance', $c_data['money']);
            $p_balance = Db::table('slzd_company')->where('id',1)->value('balance');
            $p_data['money'] = ($order_data['base_price'])*0/100;
            $p_data['companyId'] = 1;
            $p_data['type'] = 1;
            $p_data['correlationName'] = '订单收入';
            $p_data['correlationCode'] = $order_data['order_id'];
            $p_data['created_at'] = $order_data['time4'] + 30*60;
            $p_data['updated_at'] = $order_data['time4'] + 30*60;
            $p_data['balance'] = $p_balance + $p_data['money'];
            Db::table('slzd_company_money_log')->insert($p_data);
            //给总公司加钱
            Db::table('slzd_company')->where('id', 1)->setInc('balance', $p_data['money']);
                $res = Db::table('slzd_order_rrb')
                    ->where(['id' => $order_res['id']])
                    ->update(['status' => 5, 'time5' => $order_data['time4'] + 30*60, 'updated_at' => $order_data['time4'] + 30*60,'commission_driver'=>$data['money'],'commission_sys'=>$p_data['money'],'commission'=>$c_data['money']]);
                    if($order_data['buy_type'] == 3){
                           $sh_res = Db::table('slzd_sh_order')
                            ->where(['order_id' => $order_data['oid']])
                            ->update(['order_status' => 4]); 
                            $sh_order_data = Db::table('slzd_sh_order')
                            ->where(['order_id' => $order_data['oid']])
                            ->find(); 
                            $sh_admin_balance = Db::name('sh_admin')->where('admin_id',$sh_order_data['seller_id'])->value('balance');
                            $money_data['admin_id'] = $sh_order_data['seller_id'];
                            $money_data['type'] = 1;
                            $money_data['money'] = $sh_order_data['goods_price']*100;
                            $money_data['balance'] = $sh_admin_balance + $sh_order_data['goods_price']*100;
                            $money_data['traout_no'] = $sh_order_data['order_sn'];
                            $money_data['created_at'] = time();
                            $money_data['updated_at'] = time();
                            $money_data['add_day'] = date('Y/m/d',time());
                            $money_data['addtime'] = date('H/i/s',time());
                            $add_money =  db::name('sh_admin_money_log')->insert($money_data);//订单操作记录
                            $admin_res = db::name('sh_admin')->where('admin_id',$sh_order_data['seller_id'])->setInc('balance',$sh_order_data['goods_price']*100);
                        }
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            if($alias){
                aliPush([
                    'alias' => $alias,
                    'title' => '用户已经确认完成',
                    'body' => '用户已经确认完成',
                    'code' => '66816',// 人人帮下单
                    'data' =>  [
                        'code' => '66816',// 人人帮下单
                        'data' => $order_data,
                        'sound' => '用户已经确认完成'
                    ],
                ]);// 阿里云推送给d端用户
            }
        }else{
            Db::startTrans();
            try{
            $field = [
                'a.id,a.begin', 'tip','a.end','a.cid', 'uid','base_price','did','a.time', 'a.order_id','avatar','b.evaluationScore','a.type','a.number','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin', 'b.avatar', 'a.service_type','buy_type','time4','oid'
            ];
            $order_data = Db::name('order_rrb a')
                ->join('slzd_user b','a.uid = b.id')
                ->field($field)
                ->where(['a.id' => $order_res['id']])->order('a.id desc')->find();
                $order_data['base_price'] = $order_data['base_price'] - $order_data['tip'];
            $order_data['time'] = $order_data['service_type'] == 1 ? '立即送达' : date('Y-m-d H:i:s',$order_data['time']);
            $order_data['avatar'] = config('path.userimg').$order_data['avatar'];
            $order_data['title'] = '用户已经确认完成';
            $alias = Db::table('slzd_driver')->where(['driverId'=>$order_data['did']])->value('alias');
            //给司机加钱
            $data['balance'] = Db::table('slzd_driver')->where('driverId', $order_data['did'])->value('balance');
            Db::table('slzd_driver')->where('driverId', $order_data['did'])->setInc('carpoolTimes', 1);
            Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('orderNum', 1);
            Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('point',floor($order_data['base_price']/100));
                if($order_data['type'] == 2){
                    $data['money'] = ($order_data['base_price'])/100*80 + $order_data['tip'];
                    }else{
                        if($order_data['type'] == 1 && $order_data['buy_type'] == 3){
                            $data['money'] = ($order_data['base_price'])/100*80;
                        }else{
                            $data['money'] = ($order_data['base_price'])/100*80 + $order_data['tip'];
                        }
                    }
            $data['driverId'] = $order_data['did'];
            $data['type'] = 1;
            $data['correlationName'] = '订单收入';
            $data['correlationCode'] = $order_data['order_id'];
            $data['created_at'] = $order_data['time4'] + 30*60;
            $data['updated_at'] = $order_data['time4'] + 30*60;
            $data['balance'] = $data['balance'] + $data['money'];
            Db::table('slzd_driver_money_log')->insert($data);
            Db::table('slzd_driver')->where('driverId', $order_data['did'])->setInc('balance', $data['money']);
            if($order_data['cid'] == ''){
                $order_data['cid'] == 1;
            }
            $balance = Db::table('slzd_company')->where('id',$order_data['cid'])->value('balance');
            $c_data['money'] = ($order_data['base_price'])*20/100;
            $c_data['companyId'] = $order_data['cid'];
            $c_data['type'] = 1;
            $c_data['correlationName'] = '订单收入';
            $c_data['correlationCode'] = $order_data['order_id'];
            $c_data['created_at'] = $order_data['time4'] + 30*60;
            $c_data['updated_at'] = $order_data['time4'] + 30*60;
            $c_data['balance'] = $balance + $c_data['money'];
            Db::table('slzd_company_money_log')->insert($c_data);
           //给代理加钱
            Db::table('slzd_company')->where('id', $order_data['cid'])->setInc('balance', $c_data['money']);
            $p_balance = Db::table('slzd_company')->where('id',1)->value('balance');
            $p_data['money'] = ($order_data['base_price'])*0/100;
            $p_data['companyId'] = 1;
            $p_data['type'] = 1;
            $p_data['correlationName'] = '订单收入';
            $p_data['correlationCode'] = $order_data['order_id'];
            $p_data['created_at'] = $order_data['time4'] + 30*60;
            $p_data['updated_at'] = $order_data['time4'] + 30*60;
            $p_data['balance'] = $p_balance + $p_data['money'];
            Db::table('slzd_company_money_log')->insert($p_data);
            //给总公司加钱
            Db::table('slzd_company')->where('id', 1)->setInc('balance', $p_data['money']);
                $res = Db::table('slzd_order_rrb')
                    ->where(['id' => $order_res['id']])
                    ->update(['status' => 5, 'time5' => $order_data['time4'] + 30*60, 'updated_at' => $order_data['time4'] + 30*60,'commission_driver'=>$data['money'],'commission_sys'=>$p_data['money'],'commission'=>$c_data['money']]);
                    if($order_data['buy_type'] == 3){
                           $sh_res = Db::table('slzd_sh_order')
                            ->where(['order_id' => $order_data['oid']])
                            ->update(['order_status' => 4]); 
                            $sh_order_data = Db::table('slzd_sh_order')
                            ->where(['order_id' => $order_data['oid']])
                            ->find(); 
                            $sh_admin_balance = Db::name('sh_admin')->where('admin_id',$sh_order_data['seller_id'])->value('balance');
                            $money_data['admin_id'] = $sh_order_data['seller_id'];
                            $money_data['type'] = 1;
                            $money_data['money'] = $sh_order_data['goods_price']*100;
                            $money_data['balance'] = $sh_admin_balance + $sh_order_data['goods_price']*100;
                            $money_data['traout_no'] = $sh_order_data['order_sn'];
                            $money_data['created_at'] = time();
                            $money_data['updated_at'] = time();
                            $money_data['add_day'] = date('Y/m/d',time());
                            $money_data['addtime'] = date('H/i/s',time());
                            $add_money =  db::name('sh_admin_money_log')->insert($money_data);//订单操作记录
                            $admin_res = db::name('sh_admin')->where('admin_id',$sh_order_data['seller_id'])->setInc('balance',$sh_order_data['goods_price']*100);
                        }
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            if($alias){
                aliPush([
                    'alias' => $alias,
                    'title' => '用户已经确认完成',
                    'body' => '用户已经确认完成',
                    'code' => '66816',// 人人帮下单
                    'data' =>  [
                        'code' => '66816',// 人人帮下单
                        'data' => $order_data,
                        'sound' => '用户已经确认完成'
                    ],
                ]);// 阿里云推送给d端用户
            }
        }
    }
        if ($res) {
            return ['data' => '', 'code' => 200, 'message' => '操作成功', 'status' => true];
        } else {
            return ['data' => '', 'code' => 400, 'message' => '操作失败', 'status' => false];
        }
    }

    /*
    *帮我买    帮我办    帮我送   未完成的订单
    * */
    public function QueryBwzIncomplete()
    {
        $field = [
            'a.id','a.begin', 'a.end','a.cid', 'time2','uid','tip','time4','base_price','did','a.time', 'order_id','avatar','b.evaluationScore','a.type','a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin',  'a.service_type'
        ];
        $result = Db::table('slzd_order_bwz a')
            ->join('slzd_user b','a.uid = b.id')
            ->where(['a.status' => 4])
            ->field($field)
            ->select();
        if (!$result) {
            echo ' 没有数据要执行';
        } else {
            foreach ($result as $key => $value) {
                if (($value['time4'] + 30*60) < time()) {
                    //未确认的订单
                    $this->ToBwzCompleteOrder($value);
                }
            }
        }
    }
    /*
   *帮我买    帮我办    帮我送   未完成的订单
   * */
    public function ToBwzCompleteOrder($order_res){
        Db::startTrans();
        try{
        $field = [
            'a.id,a.begin', 'a.end','a.cid','order_id', 'uid','base_price', 'did', 'a.time', 'avatar', 'b.evaluationScore', 'a.type',  'a.category_name', 'b.nickname', 'b.gender', 'b.mobile', 'a.begin', 'b.avatar', 'a.service_type'
        ];
        $order_data = Db::name('order_bwz a')
            ->join('slzd_user b', 'a.uid = b.id')
            ->field($field)
            ->where(['a.id' => $order_res['id']])
            ->find();
        $order_data['time'] = $order_data['service_type'] == 1 ? '立即送达' : date('Y-m-d H:i:s', $order_data['time']);
        $order_data['avatar'] = config('path.userimg') . $order_data['avatar'];
        $order_data['title'] = '用户已经确认完成';
            $alias = Db::table('slzd_user')->where(['id' => $order_data['did']])->value('alias');
            $data['balance'] = Db::table('slzd_user')->where('id', $order_data['did'])->value('balance');
            Db::table('slzd_user')->where('id', $order_data['did'])->setInc('orderNum', 1);
            Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('orderNum', 1);
            Db::table('slzd_user')->where('id', $order_data['uid'])->setInc('point',floor($order_data['base_price']/100));
            $data['money'] = $order_data['base_price']*70/100;
            $data['uid'] = $order_data['did'];
            $data['type'] = 6;
            $data['status'] = 1;
            $data['order'] = $this->generate();
            $data['orderid'] = $order_data['id'];
            $data['ordertype'] = 'bwz';
            $data['created_at'] = time();
            $data['updated_at'] = time();
            $data['balance'] = $data['balance']+$data['money'];
            Db::table('slzd_user_money_log')->insert($data);
            Db::table('slzd_user')->where('id', $order_data['did'])->setInc('balance', $data['money']);
            if($order_data['cid'] == ''){
                $order_data['cid'] == 1;
            }
            $balance = Db::table('slzd_company')->where('id',$order_data['cid'])->value('balance');
            $c_data['money'] = $order_data['base_price']*20/100;
            $c_data['companyId'] = $order_data['cid'];
            $c_data['type'] = 1;
            $c_data['correlationName'] = '订单收入';
            $c_data['correlationCode'] = $order_data['order_id'];
            $c_data['created_at'] = time();
            $c_data['updated_at'] = time();
            $c_data['balance'] = $balance + $c_data['money'];
            Db::table('slzd_company_money_log')->insert($c_data);
            //$log_id = Db::table('slzd_company_money_log')->where('companyId',$order_data['cid'])->order('companyLogId desc')->value('companyLogId');
            //Db::table('slzd_company_money_log')->where('companyLogId',$log_id)->setInc('balance', $c_data['money']);
            Db::table('slzd_company')->where('id', $order_data['cid'])->setInc('balance', $c_data['money']);
            $p_balance = Db::table('slzd_company')->where('id',1)->value('balance');
            $p_data['money'] = $order_data['base_price']*10/100;
            $p_data['companyId'] = 1;
            $p_data['type'] = 1;
            $p_data['correlationName'] = '订单收入';
            $p_data['correlationCode'] = $order_data['order_id'];
            $p_data['created_at'] = time();
            $p_data['updated_at'] = time();
            $p_data['balance'] = $p_balance + $p_data['money'];
            Db::table('slzd_company_money_log')->insert($p_data);
            //$p_log_id = Db::table('slzd_company_money_log')->where('companyId',1)->order('companyLogId desc')->value('companyLogId');
            //Db::table('slzd_company_money_log')->where('companyLogId',$p_log_id)->setInc('balance',$p_data['money']);
            //给总公司加钱
            Db::table('slzd_company')->where('id', 1)->setInc('balance', $p_data['money']);
            $res = Db::table('slzd_order_bwz')
                ->where(['id' => $order_res['id']])
                ->update(['status' => 5, 'time5' => time(), 'updated_at' => time(),'commission_sys'=>$p_data['money'],'commission'=>$c_data['money']]);
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        sendPush([
            'type' => 'u',
            'Content' => '用户已经确认完成',
            'Title' => '用户已经确认完成',
            'alias' => $alias,
            'data' => [
                'code' => '66816',// 人人帮下单
                'data' => $order_data,
                'sound' => '用户已经确认完成'
            ],
            'extras' => []
        ]);
        if ($res) {
            return ['data' => '', 'code' => 200, 'message' => '操作成功', 'status' => true];
        } else {
            return ['data' => '', 'code' => 400, 'message' => '操作失败', 'status' => false];
        }
    }
    /* 生成订单号 */
    public function generate()
    {
        return $order = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
}